/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Form = require("form");
	var store = require("store");
	var activeUser = store.get("user");
	var aw = require("ajaxwrapper");
	var Dialog =require("dialog");
	
    module.exports = {
    		chargeBackClick : function(data, widget){
    			var chargeform = new Form({
					parentNode: ".J-dialogform",
					autoRender: false,
					model: {
						id:"subGrid",
						layout: "2_1",
						defaultButton:false,
						items: [{
						   name: "totalPrice",
					       label: "退费总金额(元)",
					       readonly:true,
					       defaultValue: data.travelContractRefund.travelContract.price
						},{
							name: "balance",
				            label: "余额(元)",
				            readonly:true,
				            defaultValue: data.travelContractRefund.travelContract.balance == null ? 0 : data.travelContractRefund.travelContract.balance
						},{
							name: "defaultRatio",
				            label: "违约金比例(%)",
				            readonly:true,
				            defaultValue: data.returnCredentials.defaultRatio
						},{
							name: "flatCost",
				            label: "工本费(元)",
				            readonly:true,
				            defaultValue: data.returnCredentials.flatCost
						},{
							name: "returnPrice",
				            label: "应退(元)",
				            readonly:true,
				            defaultValue: data.returnCredentials.price
						},{
							name: "price",
				            label: "实退(元)",
				            validate: ["required"],
				            exValidate: function(value) {
			                	var reg = /^-([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
	                            if (!reg.test(value)) {
	                                return "请输入正确金额！";
	                            } else {
	                                return true;
	                            }
	                        }, 
				            defaultValue: data.travelContractRefund.price
						},{
							name: "operate",
				            label: "经办人",
				            readonly:true,
				            defaultValue: activeUser.name
						},{
							name: "refundDate",
				            label: "退费日期",
				            type: "date",
				            readonly:true,
				            defaultValue: moment(new Date).format("YYYY-MM-DD")
						},{
	        				name: "operator",
							type: "hidden",
							defaultValue: activeUser.pkUser,
	        			},{
	        				name: "pkTravelContractRefund",
							type: "hidden",
							defaultValue: data.travelContractRefund.pkTravelContractRefund,
	        			},{
	        				name: "version",
							type: "hidden",
	        			}]
					}
				});
				widget.set("chargeform",chargeform);
				Dialog.showComponent(chargeform,{
					defaultButton:false,
					title: "退证退费",
					setStyle: function(){
						$(".el-dialog-modal .modal").css({
							"top": "10%",
							"width":"55%",
							"left": "15%",
						});
					},
					buttons: [{
						id: "save",
						text: "确定",
						handler: function(){
							if(chargeform.valid()){
								var formData = chargeform.getData();
								var saveData = {
										pkTravelContractRefund:formData.pkTravelContractRefund,
										price: formData.price,
										operator:formData.operator,
										refundDate:formData.refundDate,
										"travelContract.pkTravelContract":data.travelContractRefund.travelContract.pkTravelContract,
										version:0
								};
								aw.saveOrUpdate("api/travelcontractrefund/saverefund",aw.customParam(saveData),function(data){
									Dialog.close();
									widget.get("grid").refresh();
								})
							}
						}
					},{
						id:"close",
						text:"取消",
						handler:function(){
							Dialog.close();
						}
					}]
				});
			},
			checkClick : function(data, widget){
				var chargeform = new Form({
					parentNode: ".J-dialogform",
					autoRender: false,
					model: {
						layout: "2_1",
						defaultButton:false,
						items: [{
							   name: "totalPrice",
						       label: "退费总金额(元)",
						       readonly:true,
						       defaultValue: data.travelContractRefund.travelContract.price
							},{
								name: "balance",
					            label: "余额(元)",
					            readonly:true,
					            defaultValue: data.travelContractRefund.travelContract.balance
							},{
								name: "defaultRatio",
					            label: "违约金比例(%)",
					            readonly:true,
					            defaultValue: data.returnCredentials.defaultRatio
							},{
								name: "flatCost",
					            label: "工本费(元)",
					            readonly:true,
					            defaultValue: data.returnCredentials.flatCost
							},{
								name: "returnPrice",
					            label: "应退(元)",
					            readonly:true,
					            defaultValue: data.returnCredentials.price
							},{
								name: "price",
					            label: "实退(元)",
					            validate: ["required"],
					            readonly:true,
					            exValidate: function(value) {
				                	var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
		                            if (!reg.test(value)) {
		                                return "请输入正确金额！";
		                            } else {
		                                return true;
		                            }
		                        }, 
					            defaultValue: data.travelContractRefund.price
							},{
								name: "operate",
					            label: "经办人",
					            readonly:true,
					            defaultValue: activeUser.name
							},{
								name: "refundDate",
					            label: "退费日期",
					            type: "date",
					            readonly:true,
					            defaultValue: moment(new Date).format("YYYY-MM-DD")
							},{
		        				name: "operator",
								type: "hidden",
								defaultValue: activeUser.pkUser,
		        			},{
		        				name: "pkTravelContractRefund",
								type: "hidden",
								defaultValue: data.travelContractRefund.pkTravelContractRefund,
		        			},{
		        				name: "version",
								type: "hidden",
		        			}]
					}
				});
				widget.set("chargeform",chargeform);
				Dialog.showComponent(chargeform,{
					title: "退证退费",
					setStyle: function(){
						$(".el-dialog-modal .modal").css({
							"top": "10%",
							"width":"55%",
							"left": "15%",
						});
					},
					buttons: [{
						id:"close",
						text:"取消",
						handler:function(){
							Dialog.close();
						}
					}]
				});
			},
			form: {
		    	searchContact:function(widget,memberCertificates,pkReturnCredentials){
		    		var fetchProperties =  "travelContract.pkTravelContract,travelContract.contactNo,travelContract.status.*,travelContract.productRule.product.pkProduct,travelContract.version," +
						"travelContract.productRule.product.name,travelContract.productType.pkProductType,travelContract.productType.name," +
		   				"travelContract.number,travelContract.price,travelContract.balance,travelContract.signUpDate,travelContract.member.personalInfo.pkPersonalInfo," +
		   				"travelContract.member.personalInfo.name,travelContract.member.personalInfo.idNumber,travelContract.member.personalInfo.phone," +
		   				"welfareccList.pkWelfarecc,welfareccList.cardNo,welfareccList.count,welfareccList.returnCertificateTotal,welfareccList.consumptionTotal,welfareccList.cashTotal,welfareccList.balance," +
		   				"appList.sequenceNumber,appList.checkTime,appList.note,appList.checker.pkUser,appList.checker.name,appList.status.*"
		   			widget.service.queryTravelcontract({
						memberCertificates:memberCertificates,
						pkReturnCredentials: pkReturnCredentials,
		           		fetchProperties:fetchProperties,
					},function(data){
						var form = widget.get("form");
						form.setValue("contractVersion",data.travelContract.version);
						form.setValue("pkTravelContract",data.travelContract.pkTravelContract);
						form.setValue("contactNo",data.travelContract.contactNo);
						form.setValue("status.value",data.travelContract.status.value);
						form.setValue("product.name",data.travelContract.productRule.product.name);
						form.setValue("productType.name",data.travelContract.productType.name);
						form.setValue("number",data.travelContract.number);
						form.setValue("totalPrice",data.travelContract.price);
						form.setValue("signUpDate",data.travelContract.signUpDate);
						form.setValue("balance",data.travelContract.balance == null ? 0 : data.travelContract.balance);
						form.setValue("name",data.travelContract.member.personalInfo.name);
						form.setValue("idNumber",data.travelContract.member.personalInfo.idNumber);
						form.setValue("phone",data.travelContract.member.personalInfo.phone);
						var coupongrid = widget.get("coupongrid");
						coupongrid.setData(data.welfareccList);
						
						
						if(pkReturnCredentials != null){
							form.setDisabled(true);
							var coupongrid = widget.get("coupongrid");
							widget.hide([".J-grid"]).show([".J-form",".J-coupongrid",".J-applicategrid"]);
							widget.get("subnav").show(["return"]).hide(["status","startDate","search"]);
							var applicategrid = widget.get("applicategrid");
							applicategrid.setData(data.appList);
						}
		 			})
				}
		    }
    };
});
